<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="批准文号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'yid', validatorRules.yid]" placeholder="请输入批准文号"></a-input>
        </a-form-item>
        <a-form-item label="药品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'yname', validatorRules.yname]" placeholder="请输入药品名称"></a-input>
        </a-form-item>
        <a-form-item label="进货数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'stocks', validatorRules.stocks]" placeholder="请输入参考进价" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="参考进价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'ypprice', validatorRules.ypprice]" placeholder="请输入参考进价" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'gysname', validatorRules.gysname]" placeholder="请输入供应商名称"></a-input>
        </a-form-item>
        <a-form-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择生产日期" v-decorator="[ 'jyscrq', validatorRules.jyscrq]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="有效日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择有效日期" v-decorator="[ 'musetime', validatorRules.musetime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'jyremarks', validatorRules.jyremarks]" placeholder="请输入备注"></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "MedicineBuyerModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        yid:{rules: [{ required: true, message: '请输入药品编号!' }]},
        yname:{rules: [{ required: true, message: '请输入药品名称!' }]},
            stocks:{rules: [{ required: true, message: '请输入进货数量!' }]},
        ypprice:{rules: [{ required: true, message: '请输入参考进价!' }]},
            gysname:{rules: [{ required: true, message: '请输入供应商名称!' }]},
            jyscrq:{rules: [{ required: true, message: '请选择生产日期!' }]},
            musetime:{rules: [{ required: true, message: '请选择有效日期!' }]},
        yremarks:{},
        },
        url: {
          addbuyer: "/medicine/medicineBuyer/add",
        },
          data: [],
      }
    },
    created () {

    },
    methods: {
        add (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'yid','yname','stocks','ypprice','gysname','jyscrq','musetime','jyremarks'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
              let httpurl=this.url.addbuyer;
              let method = 'post';
              let datas={
                  yid:this.model.yid,
                  yname:this.model.yname,
                  stocks:this.model.stocks,
                  ypprice:this.model.ypprice,
                  gysname:this.model.gysname,
                  jyscrq:this.model.jyscrq,
                  musetime:this.model.musetime,
                  jyremarks:this.model.jyremarks,
                  money:(values.stocks)*(values.ypprice),
              };
            let formData = Object.assign(datas, values); // 将json合并
              console.log("表单提交数据",formData)
              /**添加进货单*/
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'yid','yname','stocks','ypprice','gysname','jyscrq','musetime','jyremarks'))
      },
    }
  }
</script>